package com.xzx.paymentdemo.service.impl;

import com.google.gson.Gson;
import com.xzx.paymentdemo.entity.PaymentInfo;
import com.xzx.paymentdemo.enums.PayType;
import com.xzx.paymentdemo.mapper.PaymentInfoMapper;
import com.xzx.paymentdemo.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Override
    public void createPaymentInfo(String decryptData) {
        log.info("记录支付日志");
        Gson gson = new Gson();
        Map<String, Object> decryptDataMap = gson.fromJson(decryptData, HashMap.class);
        String orderNo = (String)decryptDataMap.get("out_trade_no"); 
        String transactionId = (String)decryptDataMap.get("transaction_id"); 
        String tradeType = (String)decryptDataMap.get("trade_type"); 
        String tradeState = (String)decryptDataMap.get("trade_state"); 
        Map<String, Object> amount = (Map)decryptDataMap.get("amount"); 
        Integer payerTotal = ((Double) amount.get("payer_total")).intValue();

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo)
                .setPaymentType(PayType.WXPAY.getType())
                .setTransactionId(transactionId)
                .setTradeType(tradeType)
                .setTradeState(tradeState)
                .setPayerTotal(payerTotal)
                .setContent(decryptData);
        save(paymentInfo);
    }
}
